package manager
{
	import config.ConLoader;
	import config.Constant;
	
	import flash.system.System;
	import flash.utils.Dictionary;
	import flash.utils.getTimer;
	
	import mx.core.FlexGlobals;
	import mx.managers.SystemManager;
	
	import util.ScreenUtil;
	
	import valueObject.MonsterVO;

	/**
	 *怪物管理 
	 * @author SXT
	 */
	public class MonsterManager extends BaseManager implements IFram
	{
		/**
		 *怪物对象池 
		 */
		public var pool:Vector.<MonsterVO>;
		/**
		 *当前关卡配置 
		 */
		private var levelVO:LevelVO;
		/**
		 *与本类有关的配置文件 
		 */		
		private var config:Dictionary;
		/**
		 *对象池索引 
		 */
		private var index:int;
		/**
		 *上次刷怪时间 
		 */
		private var lastTime:int = getTimer();
		/**
		 *当前波中的怪物数 
		 */		
		private var cutNum:int;
		
		public function MonsterManager()
		{
			super();
			MAX = 100;
			pool = new Vector.<MonsterVO>;
			levelVO = Game.instance.main.levelManager.levelvo;
		}
		
		public function updataConfig(level:int):void{
			config =  Game.instance.main.conl["configDic"]["monster"];
		}
		
		public function framUpdata(index:int):void
		{
			refreshMonster();
			for each(var tmp:MonsterVO in pool){
				if((!ScreenUtil.isOutScreen(tmp)) && (tmp.cutHp > 0)){
					findPath(tmp);
					if(tmp.hitTestObject(Game.instance.kingMag.king)){
						//如果碰的皇上
						Game.instance.kingMag.cutHP(tmp.attack);
						remove(tmp);
					}
				}else{
					if(tmp.parent && tmp.parent.contains(tmp)){
						remove(tmp);
					}
				}
			}
		}
		
		/**
		 * 寻路
		 * @param monster
		 */		
		private function findPath(monster:MonsterVO):void
		{
			if(monster.isLast()){//是不是组后阶段
				remove(monster);//处理销毁事件
//				removeEventListener(Event.ENTER_FRAME,group1_enterFrameHandler);
			}else{
				if(monster.estimate()){//速度是否大于距离
					//如果距离大，继续移动
					go(monster);//移动
				}else{
					to(monster);//距离不够移动一次的时候，直接到
					monster.cutPhase++;//设置当前阶段+1
					monster.setRotation(getRotation(monster));//设置朝向
				}
			}
		}
		/**
		 *	移出怪 
		 * @param monster
		 */		
		private function remove(monster:MonsterVO):void{
			if(monster.parent){
				monster.parent.removeChild(monster);
			}
			monster.release();
		}
		/**
		 *刷怪管理 
		 */		
		private function refreshMonster():void
		{
			if(levelVO.cutNum < levelVO.round){
				if((getTimer() - lastTime) >= levelVO.interval[levelVO.cutNum]*1000){							//如果间隔大于规定时间
					if(cutNum <= levelVO.num[levelVO.cutNum]){
						addMonster(1);
						lastTime = getTimer();
						cutNum++;
						levelVO.cutMonsterNum++;
						if(levelVO.bossinterval.indexOf(String(levelVO.cutMonsterNum)) != -1){					//刷新精英怪
							addMonster(levelVO.boss.shift()); 
						}
					}else{
						cutNum = 0;																										//重置当前波数的怪物数
						levelVO.cutNum++;																							//当前波数+1
					}
				}
			}
		}
		
		private function addMonster(id:int):MonsterVO{
			var monster:MonsterVO;
			if(index >= MAX){
				index = 0;
				monster = pool[index];
			}else{
				monster = new MonsterVO();
				pool.push(monster);
			}
			index++;
			monster.isActivity = true;
			monster = getConfig(String(id),monster);
			monster.id = id;
			Game.instance.mtLayer.addChild(monster);
			monster.speed += Constant.BASE_SPEED;
			return monster;
		}
		
		/**
		 *读取配置文件中对应的属性 
		 * @param id	怪物的id	
		 * @param monster
		 * @return 
		 */
		private function getConfig(id:String,monster:MonsterVO):MonsterVO{
			if((levelVO.monsters.indexOf(id) != -1) || (int(id) > 999)){									//检查id是否在当前的关卡配置中
				for (var tmp:String in  config[id]){
					try{
						monster[tmp] =config[id][tmp];
					}catch(e:Error){
						try{
							if(String(config[id][tmp]).indexOf("#") != -1){
								monster[tmp] = String(config[id][tmp]).split("#");
							}else{
								monster[tmp].push(config[id][tmp]);
							}
						}catch(e:Error){
						}
//						trace(e.message);
//						trace("monster"+"的"+tmp+"属性不存在，请核对配置文件或变量命名" );
					}
				}
				monster.cutHp = monster.hp;
//				monster.setHp(monster.cutHp);
				monster.x = levelVO.startPoint[0];
				monster.y = levelVO.startPoint[1];
				monster.path = Constant.PATH_TEST;
			}
			return monster;
		}
		/**
		 *怪物的移动 
		 * @param monster
		 */		
		private function go(monster:MonsterVO):void
		{
			// trace(cutPhase+":"+path[cutPhase][2]);//打印朝向
			switch(monster.path[monster.cutPhase][2]){
				case 8:
					monster.y -= monster.speed;
					break;
				case 6:
					monster.x +=monster. speed;
					break;
				case 2:
					monster.y += monster.speed;
					break;
				case 4:
					monster.x -= monster.speed;
					break;
			}
		}
		
		/**
		 *解决速度大于距离（直接移动到目标 ）
		 * @param monster
		 */
		private function to(monster:MonsterVO):void
		{
			monster.x = monster.path[monster.cutPhase+1][0] * Constant.WH;
			monster.y = monster.path[monster.cutPhase+1][1] * Constant.WH;
		}
		
		private function getRotation(monster:MonsterVO):Number
		{
			switch(monster.path[monster.cutPhase][2]){
				case 8:
					return 0;
				case 6:
					return 90;
				case 2:
					return 180;
				case 4:
					return 270;
			}
			return monster.rotation;
		}
	}
}